package zack.gateway.assist.domain.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import zack.gateway.assist.GatewayException;
import zack.gateway.assist.common.Result;
import zack.gateway.assist.domain.model.aggregates.ApplicationSystemRichInfo;

import java.util.HashMap;
import java.util.Map;

/**注册网关算力节点服务
 * @author A1793
 */
public class GatewayCenterService {


    private Logger logger = LoggerFactory.getLogger(GatewayCenterService.class);

    public void doRegister(String address,String groupId,String gatewayId,String gatewayName,String gatewayAddress){
        Map<String, Object> map = new HashMap<>();
        map.put("groupId", groupId);
        map.put("gatewayId", gatewayId);
        map.put("gatewayName", gatewayName);
        map.put("gatewayAddress", gatewayAddress);
        String resultStr = null ;
        try{
            resultStr = HttpUtil.post(address + "/wg/admin/config/registerGateway", map,350000);
        }catch (Exception e){
            logger.error("网关服务注册异常，链接资源不可用：{}", address + "/wg/admin/config/registerGateway");
            throw e;
        }
        Result result = JSON.parseObject(resultStr, Result.class);

        logger.info("向网关中心注册网关算力服务 gatewayId：{} gatewayName：{} gatewayAddress：{} 注册结果：{}", gatewayId, gatewayName, gatewayAddress, resultStr);
        if (!"0000".equals(result.getCode())) {
            throw new GatewayException("网关服务注册异常 [gatewayId：" + gatewayId + "] 、[gatewayAddress：" + gatewayAddress + "]");
        }
    }

    public ApplicationSystemRichInfo pullApplicationSystemRichInfo(String address,String gatewayId,String systemId) {
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("gatewayId", gatewayId);
        parameterMap.put("systemId", systemId);
        String resultStr = null;
        try{
            resultStr =  HttpUtil.post(address + "/wg/admin/config/queryApplicationSystemRichInfo", parameterMap,3500000);
        }catch (Exception e){
            logger.error("网关服务拉取异常，链接资源不可用：{}", address + "/wg/admin/config/queryApplicationSystemRichInfo");
            throw e;
        }
        Result<ApplicationSystemRichInfo> result = JSON.parseObject(resultStr, new TypeReference<Result<ApplicationSystemRichInfo>>() {
        });
        if (!"0000".equals(result.getCode())) {
            throw new GatewayException("从网关中心拉取应用服务和接口的配置信息到本地完成注册异常 [gatewayId：" + gatewayId + "]");
        }
        return result.getData();
    }

    public Map<String,String> queryRedisConfig(String address){

        String resultStr = null;
        try{
            resultStr   = HttpUtil.post(address + "/wg/admin/config/queryRedisConfig", "", 3500000);
        }catch (Exception e){
            logger.error("网关服务拉取配置异常，链接资源不可用：{}", address + "/wg/admin/config/queryRedisConfig", e);
            throw e;
        }
        Result<Map<String, String>> result = JSON.parseObject(resultStr, new TypeReference<Result<Map<String, String>>>() {
        });
        logger.info("从网关中心拉取Redis配置信息完成。result：{}", resultStr);
        if(!"0000".equals(result.getCode())){
            throw new GatewayException("从网关中心拉取Redis配置信息异常");
        }
        return result.getData();
    }
}
